<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="../../../common/common-jstl.jspf" %>
<%@ include file="../../../common/common-js.jspf" %>
<!DOCTYPE html>
<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <style type="text/css">
        .container {
            width: 100%;
        }

        .title {
            width: 100%;
            background-color: #5E5E5E;
        }

        .title > span {
            color: white;
            font-size: 16px;
            font-family: "黑体";
            line-height: 32px;
            text-indent: 1em;
            display: block;
        }

        .content {
            width: 100%;
            background-color: #F5F5F5;
        }

        .container .menu, .container .menu .submenu {
            list-style: none;
        }

        .container .menu {
            margin: 0;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            $("div>input").bind("click", function () {
                //获取当前复选框的状态
                var $status = $(this).is(":checked");
                //获取当前主菜单下子菜单复选框对象
                $cks = $(this).parent().next().find("input");
                $cks.each(function () {
                    //给对应的复选框加上选择属性
                    $(this).prop("checked", $status);
                });
            });
            //给子菜单注册事件
            $("li>input").bind("click", function () {
                //获取子菜单对应主菜单
                var $mainMenu = $(this).parent().parent().prev().children("input");
                //获取子菜单选择的个数
                var count = $mainMenu.parent().next().find("input:checked").length
                if (count > 0) {
                    $mainMenu.prop("checked", true);
                } else {
                    $mainMenu.prop("checked", false);
                }
            });

            //给授权按钮注册事件
            $(".grant").bind("click", function () {
                //获取授权板块中所有选中的复选框
                var $cks = $(".content :checkbox:checked");
                //定义存储所有选中获取的权限编号组成的字符串
                var ids = "";
                //获取每一个选中复选框的值
                $cks.each(function () {
                    ids += $(this).val() + ",";
                });
                //去掉ids最后的“,”
                ids = ids.substring(0, ids.length - 1);
                //获取角色编号
                var roleId = "${param.roleId}";

                /* 			var url = "
                ${pageContext.request.contextPath }/rightInfoController";
                 $.get(url,{flag:"grant",ids:ids,roleId:roleId},function(data) {
                 if (data=="ok") {
                 layer.msg("授权成功");
                 } else {
                 layer.msg("授权失败");
                 }
                 }); */
                // 向rightInfoServlet类发送授权请求
                location.href = "${pageContext.request.contextPath }/rightInfoController?flag=grant&ids=" + ids + "&roleId=" + roleId;
            });
        });
    </script>
</head>
<body>
<div class="container">
    <div class="title"><span>角色权限列表</span></div>
    <div class="content">
        <ul class="menu">
            <c:forEach items="${mainMenus }" var="mainMenu">
                <li>
                    <div>
                        <c:set var="flag" value="false"/>
                        <c:forEach items="${mainMenuByRole }" var="mainMenuByRole">
                            <c:if test="${mainMenuByRole.rightId==mainMenu.rightId }">
                                <input type="checkbox" checked="checked"
                                       value="${mainMenu.rightId }"/>${mainMenu.rightName }
                                <c:set var="flag" value="true"/>
                            </c:if>
                        </c:forEach>
                        <c:if test="${!flag }">
                            <input type="checkbox" value="${mainMenu.rightId }"/>${mainMenu.rightName }
                        </c:if>
                    </div>
                    <ul class="submenu">
                        <c:forEach items="${childMenus }" var="childMenu">
                            <c:if test="${mainMenu.rightId==childMenu.parentId }">
                                <li>
                                    <c:set var="flag" value="false"/>
                                    <c:forEach items="${childMenuByRole }" var="childMenuByRole">
                                        <c:if test="${childMenu.rightId==childMenuByRole.rightId }">
                                            <input type="checkbox" checked="checked"
                                                   value="${childMenu.rightId }"/>${childMenu.rightName }
                                            <c:set var="flag" value="true"/>
                                        </c:if>
                                    </c:forEach>
                                    <c:if test="${!flag }">
                                        <input type="checkbox" value="${childMenu.rightId }"/>${childMenu.rightName }
                                    </c:if>
                                </li>
                            </c:if>
                        </c:forEach>
                    </ul>
                </li>
            </c:forEach>
        </ul>
        <input type="button" value="  授 权  " class="grant"/>
    </div>
</div>
</body>
</html>